/*
 * Copyright (C) 2006-2012 TongYan Corporation
 * All rights reserved.
 *
 * @brief info:
 *
 * @author: 王哲成 - wangzhecheng@yeah.net
 * @date: 2021.05.25
 * @last modified: 2021-05-25 10:40
 *
 */

#include "../include/SteepDescentMethod.hpp"

bool SteepestDescentMethod::updateSearchPoint(const ParamSet &param_set,
                                              Candidate &candidate) const {
  // 计算梯度矩阵Jx
  Vector Jx = m_ptrTarFunc->gradientVector(candidate.first, param_set.dx());
  // 计算Jx的范数
  double norm = Jx.squaredNorm();
  // 根据Jx的范数和阈值比较判断是否收敛
  if (norm < m_dEpsilon * m_dEpsilon) {
    // 如果不收敛，计算下一步的delta_x
    Vector dk = -Jx; // 梯度反方向肯定使得fx下降
    double lambda = Armijo(candidate.first, param_set.dx(), dk);
    candidate.first += dk * lambda;
    candidate.second = m_ptrTarFunc->functionValue(candidate.first);
    return true;
  }

  return false;
}
